function AddNewNote(){
	var nowDate = Date()
	var newNote = {
		'id': root.notebook.list.length+1,
		'title': '',
		'description': '',
		'priority': 1000,
		'createTime': nowDate,
		'editTime': nowDate,
		'content': ""
	}
	root.notebook.list.unshift(newNote)
	SaveRootData()
	return newNote
}
function RerenderAllNotes(){
	$("#notes-container").html('')
	for( index in root.notebook.list ){
		var nowNote = root.notebook.list[index]
		var renderedHTML = template('noteTemplate',{'t':nowNote})
		$("#notes-container").prepend(renderedHTML)
	}
}
function DeleteNote(id){
	root.notebook.list.forEach((nowNote,index,array) => {
		if( nowNote.id == id )
			array.splice(index,1)
	});
	RerenderAllNotes()
	SaveRootData()
}
function SortNotes(keyword){
	if( keyword == 'priority' ){
		root.notebook.list.sort( (a,b)=>(b.priority-a.priority) )
	}
}
function SubmitNote(id){
	var data = DumpFormData($(`#note-${id}-form`));
	root.notebook.list.forEach((nowNote,index,array) => {
		if( nowNote.id == id ){
			array[index] = data
		}
	});
	console.log(data);
	SaveRootData()
	$(`#note-${id}-submit-button`).siblings("div.label").fadeIn(200)
	$(`#note-${id}-submit-button`).siblings("div.label").fadeOut(200)
}

function InitNotes(){
	$("#add-new-note-button").click(function(){
		$("#add-new-note-button").siblings("div.label").fadeIn(200)
		var newNote = AddNewNote()
		$("#notes-container").html(
			template('noteTemplate',{'t':newNote}) + $("#notes-container").html()
		)
		$("#add-new-note-button").siblings("div.label").fadeOut(200)
	})

	SortNotes('priority')
	RerenderAllNotes()

}
